<?php

namespace common\components;

use Yii;
use common\models\City;

class ShebaoHelper {

    //  查询相关城市列表
    public static function Relate($provice, $city) {
        $relate = City::find()->select(['name', 'provice', 'city'])
                ->where(['status' => 1])->andWhere(['provice' => $provice])
                ->andWhere(['!=', 'city', $city])->asArray()->all();
        return $relate;
    }

    // 查询省份列表
    public static function Provice() {
        $provice = City::find()->select(['name', 'provice'])->where(['city' => 'index'])->asArray()->all();
        return $provice;
    }

    // 获取省份下的城市列表
    public static function City($provice) {
        $citys = City::find()->select(['name', 'provice', 'city'])->where(['status' => 1])->andWhere(['provice' => $provice])->andWhere(['!=', 'city', 'index'])->asArray()->all();
        return $citys;
    }

    // 获取各区域下的省份列表
    public static function Aera($area) {
        $areas = City::find()->select(['name', 'provice'])->where(['area' => $area])->asArray()->all();
        return $areas;
    }

    // 获取type中文名
    public static function Type($type) {
        $cat = Yii::$app->params['cat_model'];
        foreach ($cat as $value) {
            if ($value['name'] == $type) {
                $name = $value['label'];
            }
        }
        return $name;
    }

    // 更新浏览量 帮助函数
    public static function updateViews($model, $views, $id) {
        $views += 1;
        $connection = \Yii::$app->db2;
        $connection->createCommand("UPDATE {$model} SET views={$views} WHERE id={$id}")->execute();
    }

}
